package com.qiyuan.web.controller.essay;

import java.util.Arrays;
import java.util.List;

import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.essay.domain.bo.BlogCarouselBo;
import com.qiyuan.essay.domain.vo.BlogCarouselVo;
import com.qiyuan.essay.service.IBlogCarouselService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.qiyuan.common.annotation.RepeatSubmit;
import com.qiyuan.common.annotation.Log;
import com.qiyuan.common.core.controller.BaseController;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.core.domain.AjaxResult;
import com.qiyuan.common.core.validate.AddGroup;
import com.qiyuan.common.core.validate.EditGroup;
import com.qiyuan.common.core.validate.QueryGroup;
import com.qiyuan.common.enums.BusinessType;
import com.qiyuan.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;

/**
 * 轮播图Controller
 *
 * @author ruoyi
 * @date 2022-04-15
 */
@Validated
@Api(value = "轮播图控制器", tags = {"轮播图管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/essay/carousel")
public class BlogCarouselController extends BaseController {

    private final IBlogCarouselService iBlogCarouselService;

    /**
     * 查询轮播图列表
     */
    @ApiOperation("查询轮播图列表")
    @SaCheckPermission("essay:carousel:list")
    @GetMapping("/list")
    public TableDataInfo<BlogCarouselVo> list(@Validated(QueryGroup.class) BlogCarouselBo bo, PageQuery pageQuery) {
        return iBlogCarouselService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出轮播图列表
     */
    @ApiOperation("导出轮播图列表")
    @SaCheckPermission("essay:carousel:export")
    @Log(title = "轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BlogCarouselBo bo, HttpServletResponse response) {
        List<BlogCarouselVo> list = iBlogCarouselService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮播图", BlogCarouselVo.class, response);
    }

    /**
     * 获取轮播图详细信息
     */
    @ApiOperation("获取轮播图详细信息")
    @SaCheckPermission("essay:carousel:query")
    @GetMapping("/{id}")
    public AjaxResult<BlogCarouselVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Integer id) {
        return AjaxResult.success(iBlogCarouselService.queryById(id));
    }

    /**
     * 新增轮播图
     */
    @ApiOperation("新增轮播图")
    @SaCheckPermission("essay:carousel:add")
    @Log(title = "轮播图", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BlogCarouselBo bo) {
        return toAjax(iBlogCarouselService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改轮播图
     */
    @ApiOperation("修改轮播图")
    @SaCheckPermission("essay:carousel:edit")
    @Log(title = "轮播图", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BlogCarouselBo bo) {
        return toAjax(iBlogCarouselService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除轮播图
     */
    @ApiOperation("删除轮播图")
    @SaCheckPermission("essay:carousel:remove")
    @Log(title = "轮播图" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Integer[] ids) {
        return toAjax(iBlogCarouselService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
